package com.houmeiyuan.huiguniang.ctrl;

import com.google.gson.Gson;
import com.houmeiyuan.huiguniang.common.MessageConstVar;
import com.houmeiyuan.huiguniang.common.ParamsUtil;
import com.houmeiyuan.huiguniang.common.Result;
import com.houmeiyuan.huiguniang.modles.domain.OrderForPay;
import com.houmeiyuan.huiguniang.modles.domain.OrderForm;
import com.houmeiyuan.huiguniang.modles.domain.WXPay;
import com.houmeiyuan.huiguniang.service.OrderFormService;
//import com.houmeiyuan.huiguniang.service.PayService;
import com.houmeiyuan.huiguniang.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderFormCtrl extends BaseCtrl {
    @Autowired
    public OrderFormService orderformservice;
    @Autowired
    private ProductService productService;

    //@Autowired
    //private PayService payService;

    /**
     * addorder添加订单
     * @param request
     * @return
     */
    //@PostMapping("/addOrder")
    //public Result addOrder(HttpServletRequest request) {
    //    //收到前端回传的orderlist对象
    //    String answerJson = request.getParameter("orderList");
    //    //初始化支付订单类openId;addressId;money;openId; List<ShopCar> list;购物车的付费
    //    OrderForPay orderForPay = new OrderForPay();
    //    Gson gson = new Gson();
    //    if (answerJson != null && !answerJson.equals("")){
    //        orderForPay = gson.fromJson(answerJson, OrderForPay.class);
    //    }else {
    //        return send(MessageConstVar.Error);
    //    }
    //    try {
    //        //使用map类型接收payService生成的order内容
    //        Map<String, String> result = payService.createOrder(orderForPay);
    //
    //        if (result != null) {
    //            if (result.get("return_code").equals("SUCCESS") && result.get("result_code").equals("SUCCESS")) {
    //                //初始化微信支付tradeType;prepayId;codeUrl; sign;nonceStr;
    //                WXPay wxPay = new WXPay();
    //                wxPay.setTradeType(result.get("trade_type"));
    //                wxPay.setCodeUrl(result.get("code_url"));
    //                wxPay.setPrepayId(result.get("prepay_id"));
    //                wxPay.setNonceStr(result.get("nonce_str"));
    //                wxPay.setSign(result.get("sign"));
    //                return send(wxPay);
    //            }
    //        }
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //    }
    //    return send(MessageConstVar.Error);
    //
    //}

    /**
     * 取消订单及确认收货
     *
     * @param isPay
     * @param isActive
     * @param isReceipt
     * @param trackNumber
     * @param id
     * @return
     */
    @PostMapping("/updateOrder")
    public Result updateOrder(@RequestParam(value = "isPay", required = false) Integer isPay,
                              @RequestParam(value = "isActive", required = false) Integer isActive,
                              @RequestParam(value = "isReceipt", required = false) Integer isReceipt,
                              @RequestParam(value = "trackNumber", required = false) String trackNumber,
                              @RequestParam(value = "id",required = false) Integer id) {
        try {
            OrderForm orderForm = orderformservice.selectById(id);
            orderForm.setUpdateTime(new Date());
            if (isPay != null && isPay == 1 && orderForm.getIsPay() != 2) {
                orderForm.setIsPay(1);
                orderForm.setCreateTime(new Date());
                return send(orderformservice.updateOrder(orderForm), "支付成功");
            } else if (isPay != null && isPay == 2) {
                orderForm.setIsPay(2);
                return send(orderformservice.updateOrder(orderForm), "订单已支付");
            }
            if (orderForm.getIsPay() == 1 && isActive != null && isActive == 1) {
                orderForm.setIsActive(1);
                orderForm.setPayTime(new Date());
                return send(orderformservice.updateOrder(orderForm), "订单正常!");
            }
            if (trackNumber != null && !"".equals(trackNumber)) {
                orderForm.setTrackNumber(trackNumber);
                return send(orderformservice.updateOrder(orderForm));
            }
            if (orderForm.getIsPay() == 1 && orderForm.getIsActive() == 1 && isReceipt != null && isReceipt == 1) {
                orderForm.setIsReceipt(1);
                orderForm.setReceiptTime(new Date());
                productService.updateSellNumber(id);
            }
            return send(orderformservice.updateOrder(orderForm), "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return send(-1, "操作失败!");
        }
    }

    /**
     * 删除用户的指定订单号
     *
     * @param orderFormId
     * @param openId
     * @return
     */
    @DeleteMapping("/DeleteByOrderFormId")
    public Result DeleteByOrderFormId(@RequestParam("openId") String openId, @RequestParam("orderFormId") int orderFormId) {
        return send(orderformservice.DeleteByOrderFormId(openId, orderFormId));
    }

    /**
     * 查询所有
     *
     * @param pageStart
     * @param pageSize
     * @param openId
     * @return
     */

    @GetMapping("/selectAll")
    public Result selectAll(@RequestParam(name = "pageStart", defaultValue = "1") Integer pageStart,
                            @RequestParam(name = "pageSize", defaultValue = "7") Integer pageSize,
                            @RequestParam(name = "openId") String openId) {
        Map<String, Object> params = new HashMap<>();
        params = ParamsUtil.addPageInfo(params, pageStart, pageSize);
        if (openId != null && !"".equals(openId)) {
            params.put("openId", openId);
        }
        return send(orderformservice.selectAll(params));
    }
}
